//
//  ChangePhoneController.m
//  JianTingSwift
//
//  Created by zhibao on 2024/11/7.
//

#import "ChangePhoneController.h"
#import "LoginCodeInputController.h"
@interface ChangePhoneController (){
    QMUITextField *phoneField;
    UIButton *nextBtn;
    NSMutableArray *codesArray;
}
@end

@implementation ChangePhoneController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    
}
-(void)initView{
    self.bgImgView.hidden = NO;
    UILabel *clabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 103 - 64 + NAVIH, MY_Screen_W - 60, 35)];
    clabel.text = LocalizedString(@"更换手机号");
    clabel.font = MY_SCALE_RegularFONT(25);
    clabel.textColor = UIColor.whiteColor;
    [self.view addSubview:clabel];
    
    UILabel *cplabel = [[UILabel alloc]initWithFrame:CGRectMake(clabel.left, clabel.bottom + 3, MY_Screen_W - 60, 19)];
    cplabel.text = [NSString stringWithFormat:@"%@ %@",LocalizedString(@"您的当前手机号为"),[UserInfoManager sharedManager].phone];
    cplabel.font = MY_SCALE_RegularFONT(13);
    cplabel.textColor = MyColor(@"999999");
    [self.view addSubview:cplabel];
    
    phoneField = [QMUITextField new];
    phoneField.placeholder = LocalizedString(@"请输入手机号");
    phoneField.placeholderColor = MyColor(@"777777");
    phoneField.font = MY_SCALE_RegularFONT(14);
    phoneField.textColor = UIColor.whiteColor;
    [phoneField addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:phoneField];
    [phoneField mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(selectBtn.mas_right).offset(20);
        make.top.equalTo(cplabel.mas_bottom).offset(20);
        make.right.equalTo(self.view).offset(-30);
        make.height.mas_equalTo(36);
    }];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(30, cplabel.bottom + 61, MY_Screen_W - 60, 0.5)];
    line.backgroundColor = MyColor(@"#303339");
    [self.view addSubview:line];
    
    UILabel *ctiplabel = [[UILabel alloc]initWithFrame:CGRectMake(clabel.left, line.bottom + 10, MY_Screen_W - 60, 50)];
    ctiplabel.text = LocalizedString(@"更换手机号后，下次登录可使用新手机号登录");
    ctiplabel.font = MY_SCALE_RegularFONT(12);
    ctiplabel.numberOfLines = 0;
    ctiplabel.lineBreakMode = NSLineBreakByWordWrapping;
    ctiplabel.textColor = MyColor(@"#999999");
    [self.view addSubview:ctiplabel];
    
    nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    nextBtn.frame = CGRectMake(30, ctiplabel.bottom + 50, MY_Screen_W - 60, 45);
    nextBtn.layer.cornerRadius = 20;
    nextBtn.backgroundColor = ciThemeColor;
    [self.view addSubview:nextBtn];
    nextBtn.userInteractionEnabled = NO;
    [nextBtn setTitle:LocalizedString(@"获取验证码") forState:UIControlStateNormal];
    [nextBtn addTarget:self action:@selector(nextClick) forControlEvents:UIControlEventTouchUpInside];
    nextBtn.titleLabel.font = MY_SCALE_SemiboldFONT(15);
    [nextBtn setTitleColor:MyColor(@"ffffff") forState:UIControlStateNormal];
    if (self.type == 2) {
        clabel.text = LocalizedString(@"绑定手机号");
        cplabel.text = @"";
        ctiplabel.text = LocalizedString(@"绑定手机号后，下次登录可使用新手机号登录");
        phoneField.placeholder = LocalizedString(@"请输入手机号");
        phoneField.placeholderColor = MyColor(@"f1f1f1");
        nextBtn.backgroundColor = ciThemeColor;
        nextBtn.userInteractionEnabled = YES;
        [nextBtn setTitle:LocalizedString(@"绑定") forState:UIControlStateNormal];
    }
    YYLabel *bottomLabel = [[YYLabel alloc]initWithFrame:CGRectMake(0, SafeSCREEN_HEIGHT - 23 - 20, MY_Screen_W, 20)];
    bottomLabel.centerX = MY_Screen_W / 2;
    
    //bottomLabel.preferredMaxLayoutWidth = MY_Screen_W - 60;
    [self.view addSubview:bottomLabel];
    
    NSMutableString *string = [[NSMutableString alloc]initWithString:LocalizedString(@"登录即代表同意")];
    [string appendString:@"《"];
    [string appendString:LocalizedString(@"用户协议")];
    [string appendString:@"》"];
    [string appendString:@"和"];
    [string appendString:@"《"];
    [string appendString:LocalizedString(@"隐私政策")];
    [string appendString:@"》"];
    NSRange yhrange = [string rangeOfString:LocalizedString(@"用户协议")];
    NSRange xyrange = [string rangeOfString:LocalizedString(@"隐私政策")];
    
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:string];
    attri.yy_font = MY_SCALE_RegularFONT(11);
    attri.yy_color = UIColor.whiteColor;
    __weak typeof(self) weakSelf = self;
    [attri yy_setTextHighlightRange:NSMakeRange(yhrange.location - 1, yhrange.length + 2) color:MYRed backgroundColor:UIColor.clearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        WebViewController *controller = [[WebViewController alloc] initWithtitle:LocalizedString(@"用户协议") WithUrl:@"https://docs.qq.com/doc/DRmNvbVFaZm9LVG9t"];
        [strongSelf.navigationController pushViewController:controller animated:YES];
    }];
    
    [attri yy_setTextHighlightRange:NSMakeRange(xyrange.location - 1, xyrange.length + 2) color:MYRed backgroundColor:UIColor.clearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        WebViewController *controller = [[WebViewController alloc] initWithtitle:LocalizedString(@"隐私协议") WithUrl:@"https://docs.qq.com/doc/DRnhXbWpia3dHV0R5"];
        [strongSelf.navigationController pushViewController:controller animated:YES];
    }];
    bottomLabel.attributedText = attri;
    bottomLabel.textAlignment = NSTextAlignmentCenter;
}
-(void)nextClick{

    if(phoneField.text.qmui_trim.length < 6){
        [QMUITips showError:LocalizedString(@"请输入正确手机号！")];
        return;
    }
    [QMUITips showLoadingInView:self.view];
    NSDictionary *dic =@{
        @"phone":phoneField.text.qmui_trimAllWhiteSpace,
        @"type":@"1"
    };
    [[HttpHelper sharedInstance] sendPost:@"api/short/message" parameters:dic success:^(NSURLSessionTask * _Nonnull operation, ResponseModel * _Nonnull responseObject) {
        DLog(@"code===%@",responseObject.data);
        [QMUITips hideAllTipsInView:self.view];
        if (responseObject.code == 1) {
            LoginCodeInputController *controller = [LoginCodeInputController new];
            controller.type = self.type;
            controller.phone = self->phoneField.text.qmui_trimAllWhiteSpace;
            [self.navigationController pushViewController:controller animated:YES];
        }else{
            [QMUITips showError:responseObject.message];
        }
        
    } failure:^(NSURLSessionTask * _Nonnull operation, NSError * _Nonnull error) {
        [QMUITips hideAllTipsInView:self.view];
    }];
}

-(void)textChange:(QMUITextField *)field{
    DLog(@"wwww===%@",field.text);
    if (field.text.qmui_trimAllWhiteSpace.length < 6) {
        nextBtn.backgroundColor = ciThemeColor;
        nextBtn.userInteractionEnabled = NO;
    }else{
        nextBtn.backgroundColor = MYRed;
        nextBtn.userInteractionEnabled = YES;
    }
}

@end
